﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Hotel
{
    public partial class ServiceForRoom : Form
    {
        public ServiceForRoom()
        {
            InitializeComponent();
        }
        CNDatabase cnDb = new CNDatabase();
        string idListRoom = "0";
        private void ServiceForRoom_Load(object sender, EventArgs e)
        {
            string qr = "select distinct NameRoom, Room.IDRoom from BookingLog, Room, ListRoomBooking where ListRoomBooking.IDRoom= Room.IDRoom "
                + "and ListRoomBooking.IDBookingLog = BookingLog.IDBookingLog "
                + "and TypeBooking='Staying'and TotalAmount =0 order by NameRoom ASC";
            BindingSource bdSRoom = new BindingSource();
            bdSRoom = cnDb.getData(qr);
            cbListRoom.DataSource = bdSRoom;
            cbListRoom.DisplayMember = "NameRoom";
            cbListRoom.ValueMember = "IDRoom";
            //Type Service
            //select distinct TypeService from Service
            BindingSource bdSTypeServive = new BindingSource();
            bdSTypeServive.DataSource = cnDb.getData("select distinct TypeService from Service order by TypeService ASC");
            cbType.DataSource = bdSTypeServive;
            cbType.DisplayMember = "TypeService";
            cbType.ValueMember = "TypeService";


        }

        private void cbListRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string qr = " from Customer,BookingLog, ListRoomBooking "
                    + "where Customer.IdentificationNumber=BookingLog.IdentificationNumber "
                    + "and ListRoomBooking.IDBookingLog=BookingLog.IDBookingLog and TotalAmount=0 "
                    + "and TypeBooking='Staying' and ListRoomBooking.IDRoom = '" + cbListRoom.SelectedValue + "' ";
                lbName.Text = cnDb.getOneData("select FullName " + qr).ToString();
                lbIdentify.Text = cnDb.getOneData("select BookingLog.IdentificationNumber " + qr).ToString();
                // MessageBox.Show("ID ListRoom: " + cnDb.getOneData("select IDListRoom " + qr).ToString());
                idListRoom = cnDb.getOneData("select IDListRoom " + qr).ToString();


                // List service of Room
                loadService();


            }
            catch (Exception)
            {
                cnDb.closeConnect();
            }
        }

        private void cbNameService_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                txtDesc.Text = cnDb.getOneData("select Description from Service where IDService='" + cbNameService.SelectedValue + "'").ToString();
                txtPrice.Text = cnDb.getOneData("select Price from Service where IDService='" + cbNameService.SelectedValue + "'").ToString();
                lbUnit.Text = cnDb.getOneData("select Unit from Service where IDService='" + cbNameService.SelectedValue + "'").ToString();
            }
            catch (Exception)
            {
                cnDb.closeConnect();
            }
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                BindingSource bdSServive = new BindingSource();
                bdSServive.DataSource = cnDb.getData("select * from Service where TypeService= '" + cbType.SelectedValue + "' order by NameService DESC");
                cbNameService.DataSource = bdSServive;
                cbNameService.DisplayMember = "NameService";
                cbNameService.ValueMember = "IDService";
            }
            catch (Exception)
            {
                cnDb.closeConnect();
            }
        }

       
        private void loadService()
        {
            string qr = "select Date, NameService, Price, Unit, Quantity, LogService.Cost from Service "
                + " join LogService on Service.IDService = LogService.IDService "
                + " join ListRoomBooking on LogService.IDListRoom= ListRoomBooking.IDListRoom "
                + " join BookingLog on ListRoomBooking.IDBookingLog= BookingLog.IDBookingLog "
                + " where TypeBooking ='Staying' and TotalAmount=0 and IDRoom= '" + cbListRoom.SelectedValue + "'";
            BindingSource bdSGrv = new BindingSource();
            bdSGrv.DataSource = cnDb.getData(qr);
            grvService.DataSource = bdSGrv;
            //grvService.DataBindings();
            string qrTotal = "select SUM(LogService.Cost) "
                + " from Service "
                + " join LogService on Service.IDService = LogService.IDService "
                + " join ListRoomBooking on LogService.IDListRoom= ListRoomBooking.IDListRoom "
                + " join BookingLog on ListRoomBooking.IDBookingLog= BookingLog.IDBookingLog "
                + " where TypeBooking ='Staying' and TotalAmount=0 and IDRoom= '" + cbListRoom.SelectedValue + "'";
            lbTotal.Text = cnDb.getOneData( qrTotal).ToString();
        }

        private void btAddService_Click(object sender, EventArgs e)
        {
            //Add
            cnDb.connect();
            SqlCommand cmd = new SqlCommand("sp_ServiceOfRoom", cnDb.cn);
            cmd.CommandText = "sp_ServiceOfRoom";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@IDListRoom", idListRoom);
            cmd.Parameters.AddWithValue("@IDService", cbNameService.SelectedValue);
            cmd.Parameters.AddWithValue("@Quantity", txtQuantity.Text);
            cmd.ExecuteNonQuery();
            cnDb.closeConnect();
            loadService();
        }





    }
}
